package com.example.nbaplayerssb.controller;

import com.example.nbaplayerssb.pojo.Clubs;
import com.example.nbaplayerssb.pojo.Players;
import com.example.nbaplayerssb.service.PlayersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.util.List;


@Controller
@RequestMapping
public class PlayersController extends BaseController{
    @Autowired
    private PlayersService playersService;

    /**
     * 查询球员信息(模糊查询)
     * @param model
     * @param pname
     * @return
     */
    @GetMapping("/list")
    public String list(Model model, @RequestParam(required = false) String pname,
                       @RequestParam(required = false) Integer cid){

        List<Players> list = playersService.getList(pname,cid);

        model.addAttribute("list", list);

        return "list";//转发
    }


    /**
     * 查询球队信息
     * @param model
     * @param clubs
     * @return
     */
    @GetMapping("/listClubs")
    public String listClubs(Model model, Clubs clubs){
        List<Clubs> listClubs = playersService.getClubs(clubs);

        model.addAttribute("listClubs", listClubs);

        return "list";//转发
    }


    /**
     * 新增
     * @param players
     * @return
     */
    @PostMapping("/add")
    public String add(Players players){
        Integer add = playersService.getAdd(players);

        if (add>0){
            return "redirect:list";//重定向
        }

        return "add";
    }

    @GetMapping("/add")
    public String add(){

        return "add";
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public String delete(Integer id){
        Integer delete = playersService.getDelete(id);

        if (delete>0){
            return "redirect:list";//重定向
        }

        return "list";
    }


    /**
     * 查询单条记录
     * @param model
     * @param id
     * @return
     * @throws ParseException
     */
    @GetMapping("/byId")
    public String byId(Model model,Integer id) throws ParseException {
        Players players = playersService.getById(id);
        model.addAttribute("players",players);
        return "update";
    }


    /**
     * 更新
     * @param players
     * @return
     */
    @PostMapping("/update")
    public String update(Players players) {
        Integer update = playersService.update(players);
        if (update>0){
            return "redirect:list";//重定向
        }
        return "update";
    }

}
